{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import seaborn as sbn\n",
    "import gc\n",
    "#gc.collect()\n",
    "#建模需要导入的库\n",
    "from sklearn.linear_model import LogisticRegression as LR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#输入文件\n",
    "DATA_PATH = os.path.join(os.getcwd(),'data','simply_id','input')\n",
    "NEW_TRAIN  =  os.path.join(DATA_PATH,'new_train.csv')\n",
    "NEW_TEST = os.path.join(DATA_PATH,'new_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.read_csv(NEW_TRAIN)\n",
    "test = pd.read_csv(NEW_TEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = train[['target','song_id_re_rate', 'song_length_re_rate', 'song_year_re_rate',\n",
    "       'first_genre_id_re_rate', 'artist_name_id_re_rate',\n",
    "       'lyricist_id_re_rate', 'language_id_re_rate','msno_re_rate','city_re_rate', 'registered_via_re_rate',\n",
    "          'registration_init_time_re_rate','op_type_re_rate' ]]\n",
    "test = test[['target','song_id_re_rate', 'song_length_re_rate', 'song_year_re_rate',\n",
    "       'first_genre_id_re_rate', 'artist_name_id_re_rate',\n",
    "       'lyricist_id_re_rate', 'language_id_re_rate','msno_re_rate','city_re_rate', 'registered_via_re_rate',\n",
    "          'registration_init_time_re_rate','op_type_re_rate' ]]\n",
    "train = train.fillna(train.mean())\n",
    "test = test.fillna(test.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train = train.drop(['target'], axis=1).values\n",
    "y_train = train['target'].values\n",
    "X_test = test.drop(['target'], axis=1).values\n",
    "y_test = test['target'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 34.3 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
       "          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
       "          verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr = LR()   #建立逻辑回归模型\n",
    "%time lr.fit(X_train,y_train)  #用筛选后的特征数据来训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.67681679722390897"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.score(X_test,y_test)   #给出模型的平均正确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1432368  840961]\n",
      " [ 698144 1896378]]\n",
      "[[353976 214034]\n",
      " [179268 469685]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "from scipy import interp \n",
    "cm_train = metrics.confusion_matrix(y_train,lr.predict(X_train))  #训练样本的混淆矩阵\n",
    "print(cm_train)\n",
    "cm_test = metrics.confusion_matrix(y_test,lr.predict(X_test))  #训练样本的混淆矩阵\n",
    "print(cm_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-10-0fc2f4c5e94f>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-10-0fc2f4c5e94f>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    test_rate = metrics.roc_auc_score(y_train,lr.predict(X_train)\u001b[0m\n\u001b[1;37m                                                                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "train_rate = metrics.roc_auc_score(y_train,lr.predict(X_train))\n",
    "test_rate = metrics.roc_auc_score(y_train,lr.predict(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1432368  840961]\n",
      " [ 698144 1896378]]\n",
      "[[353976 214034]\n",
      " [179268 469685]]\n"
     ]
    }
   ],
   "source": [
    "#模型评价\n",
    "from sklearn import metrics\n",
    "from scipy import interp \n",
    "cm_train = metrics.confusion_matrix(y_train,lr.predict(X_train).round())  #训练样本的混淆矩阵\n",
    "print(cm_train)\n",
    "cm_test = metrics.confusion_matrix(y_test,lr.predict(X_test).round())  #训练样本的混淆矩阵\n",
    "print(cm_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
